package com.simon.system.controller;

import java.util.List;

import com.simon.exception.OperationFailedException;
import com.simon.http.model.DoResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.simon.system.model.Permisson;
import com.simon.system.service.PermissonService;
import com.simon.token.TokenValidate;
import com.simon.utils.SystemCode;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * @ClassName MenuController
 * @Description 菜单&权限控制层
 * @Author Jiajie.Mao
 * @Date 2018/6/5 9:38
 * @Version 1.0
 **/
@RestController
@RequestMapping(value = "/menu")
@Api(value = "menu", tags = {"菜单接口"})
public class MenuController {

    @Autowired
    private PermissonService permissonService;


    @ApiOperation(value = "添加菜单", notes = "根据Menu对象创建菜单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "身份令牌", paramType = "header", dataType = "String"),
            @ApiImplicitParam(name = "menu", value = "菜单对象", required = true, dataType = "Permisson")
    })
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @TokenValidate
    public DoResponse<?> addMenu(@RequestBody Permisson menu) {
        Permisson dMenu = permissonService.findByCode(menu.getCode());
        if (dMenu != null) {
            throw new OperationFailedException("该菜单/动作已存在");
        } else {
            //插入数据库
            int res = permissonService.addMenu(menu);
            if (res > 0) {
                return new DoResponse<>();
            } else {
                throw new OperationFailedException();
            }
        }
    }

    @ApiOperation(value = "查询菜单", notes = "根据id获取菜单信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "身份令牌",  paramType = "header", dataType = "String"),
            @ApiImplicitParam(name = "id", value = "菜单id", required = true, dataType = "int")
    })
    @RequestMapping(value = "/get", method = RequestMethod.GET)
	@TokenValidate
    public DoResponse<?> getMenuById(@RequestParam int id) {
        Permisson permisson = permissonService.getMenuById(id);
        if (permisson != null) {
            return new DoResponse<>(permisson);
        } else {
            throw new OperationFailedException();
        }
    }

    @ApiOperation(value = "获取菜单列表", notes = "")
    @ApiImplicitParam(name = "Authorization", value = "身份令牌",  paramType = "header", dataType = "String")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @TokenValidate
    public DoResponse<?> findAllMenu() {
        List<Permisson> menus = permissonService.findAllMenus();
        if (menus.size() > 0) {
            return new DoResponse<>(menus);
        } else {
            throw new OperationFailedException();
        }
    }


    @ApiOperation(value = "删除菜单", notes = "根据用户id删除菜单信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "身份令牌", paramType = "header", dataType = "String"),
            @ApiImplicitParam(name = "id", value = "菜单id", required = true, dataType = "int")
    })
    @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
    @TokenValidate
    public DoResponse<?> deleteMenu(@RequestParam int id) {
        int res = permissonService.deleteMenu(id);
        if (res > 0) {
            return new DoResponse<>();
        } else {
            throw new OperationFailedException();
        }
    }


    @ApiOperation(value = "更新菜单", notes = "更新菜单信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "身份令牌", paramType = "header", dataType = "String"),
            @ApiImplicitParam(name = "id", value = "菜单id", required = true, dataType = "int"),
            @ApiImplicitParam(name = "menu", value = "菜单对象", required = true, dataType = "Permisson")
    })
    @RequestMapping(value = "/edit", method = RequestMethod.PUT)
    @TokenValidate
    public DoResponse<?> updateMenu(@RequestParam int id, @RequestBody Permisson menu) {
        Permisson dMenu = permissonService.getMenuById(id);
        if (dMenu != null) {
            int res = permissonService.updateMenu(dMenu, menu);
            if (res > 0) {
                return new DoResponse<>();
            } else {
                throw new OperationFailedException();
            }

        } else {
            throw new OperationFailedException("不存在此数据");
        }
    }
}
